package com.eonmind.ai.core.video.platform.pixverse;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.eonmind.ai.core.video.model.AiVideoTaskResModel;
import com.eonmind.ai.core.video.platform.enums.AiVideoPlatformEnum;
import com.eonmind.ai.core.video.platform.pixverse.api.AbstractPixVerseService;
import com.eonmind.ai.core.video.platform.pixverse.model.PixverseTaskQueryRes;
import com.eonmind.ai.core.video.platform.pixverse.model.PixverseTaskSubmitReq;
import com.eonmind.ai.utils.HttpUtil;
import com.eonmind.common.contract.constants.ai.AIVideoPlatformApiConstant;
import com.eonmind.common.contract.exception.BusinessException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;

import java.text.MessageFormat;
import java.util.Map;

/**
 * Pixverse 平台的服务实现类
 */
@Slf4j
@Service
public class PixVerseService extends AbstractPixVerseService {
    @Resource
    private HttpUtil httpUtil;

    @Override
    public AiVideoTaskResModel submitVideoTask(AiVideoPlatformEnum aiVideoPlatform,PixverseTaskSubmitReq aiVideoTask) {
       Map<String,String> header =  buildHeader(aiVideoPlatform);
       String body =  buildTaskSubmitBody(aiVideoTask);
       // 构建Url
        String submitTaskUrl = AIVideoPlatformApiConstant.Pixverse.SUBMIT_TASK_URL;
        String baseUrl = aiVideoPlatform.getBaseUrl();
        submitTaskUrl = MessageFormat.format(submitTaskUrl,baseUrl);
        // 构建请求
        Mono<ResponseEntity<String>> responseEntityMono = httpUtil.post(submitTaskUrl, header, body);
        ResponseEntity<String> block = responseEntityMono.block();
        if (block!= null && JSONUtil.isTypeJSON(block.getBody())) {
            JSONObject bodyJson = new JSONObject(block.getBody());
            PixverseTaskQueryRes pixverseTaskQueryRes = jsonToResult(bodyJson);
            return new AiVideoTaskResModel(pixverseTaskQueryRes.getTaskId(),aiVideoPlatform.getCode());
        }else {
            log.error("绘画任务提交请求失败：{}",block);
            throw new BusinessException("请求失败");
        }
    }



    @Override
    public PixverseTaskQueryRes queryVideoTask(AiVideoPlatformEnum aiVideoPlatform, String taskId) {
        if(ObjectUtils.anyNull(taskId,aiVideoPlatform)) {
            throw new BusinessException("Pixverse 获取任务参数错误");
        }
        // 构建Url
        String queryTaskUrl = AIVideoPlatformApiConstant.Pixverse.QUERY_TASK_URL;
        String baseUrl = aiVideoPlatform.getBaseUrl();
        queryTaskUrl = MessageFormat.format(queryTaskUrl,baseUrl);

        // 构建请求头
        Map<String,String> header = buildHeader(aiVideoPlatform);

        // 构建请求体
        Map<String,String> params = buildTaskQueryParams(taskId);
        MultiValueMap<String,String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.setAll(params);
        Mono<ResponseEntity<String>> responseEntityMono = httpUtil.get(queryTaskUrl, header, paramsMap);
        ResponseEntity<String> block = responseEntityMono.block();
        if (block!= null && JSONUtil.isTypeJSON(block.getBody())) {
            JSONObject bodyJson = new JSONObject(block.getBody());
            return jsonToResult(bodyJson);
        }else {
            log.error("绘画任务查询请求失败：{}",block);
            throw new BusinessException("绘画任务查询请求失败");
        }
    }


}
